package com.example.wacoffee_manage.Mapper;

import com.example.wacoffee_manage.annotation.AutoFill;
import com.example.wacoffee_manage.common.OperationType;
import com.example.wacoffee_manage.pojo.Good;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodMapper {
    List<Good> getAllGood(Good good);

    @AutoFill(OperationType.INSERT)
    @Insert("insert into good (name,price,stock,image,description, create_time, creator, update_time, updator) values " +
            "(#{name},#{price},#{stock},#{image},#{description},#{createTime},#{creator},#{updateTime},#{updater})")
    void add(Good good);

    @Select("select g.id, g.name, g.stock, g.price, g.image, g.status, g.description,s.name as supplyName from good g left join good_supply gs on g.id=gs.good_id left join wa_coffee.supply s on gs.supply_id = s.id" +
            " where g.id=#{id}")
    Good getById(int id);

    @AutoFill(OperationType.UPDATE)
    @Update("update good set name=#{name},price=#{price},stock=#{stock},image=#{image},description=#{description},updator=#{updater},update_time=#{updateTime} where id=#{id}")
    void update(Good good);

    @Update("update good  set status=0 where id=#{id}")
    void delete(int id);

    @Select("select count(id) from supply where name=#{supplyName} ")
    int supplyIsFirstAppear(String supplyNaME);

    @Insert("insert into  supply (name) values (#{supplyName})")
    void addSupply(String supplyName);

    @Select("select id from good where name=#{name}")
    int getId(String name);

    @Select("select id from supply where name=#{supplyName}")
    int getSupplyId(String supplyName);

   @Insert("insert into  good_supply (good_id, supply_id) VALUES (#{goodId},#{supplyId})")
    void addGoodSupply(int goodId, int supplyId);

   @Delete("delete from good_supply where good_id=#{id}")
    void deleteGoodToSupply(int id);

    @Select("select g.id, g.name, g.stock, g.price, g.image,g.description from good g")
    List<Good> getAll();

    void deleteBatch(List<Long> ids);
}
